﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Kunlun.Models;
using Kunlun.BusinessLogic;
using KunLunDirModels.Model;

namespace Kunlun.Controllers
{
    public class PersonFrameController : Controller
    {
        //
        // GET: /PersonFrame/
		private static int FRAME_WIDTH = 650;
		private static int FRAME_HEIGHT = 450;
		[Authorize]
        public ActionResult Main()
        {
			string userid = Request.Cookies.Get("userId").Value;
			Dictionary<string,string> where = new Dictionary<string,string>();
			where.Add("UserID",userid);
			ViewData.Model = new FlexSave().GetUserWorks(0, 10, WorkTypeEnum.框画, where);
            return View();
        }

		[Authorize]
		public ActionResult DirFrame() {
			Product p = new Product();
			ViewData.Model = p;
			Product_attributes attr = new Product_attributes();
			int width, height;
			int.TryParse(Request.QueryString["w"], out width);
			int.TryParse(Request.QueryString["h"], out height);
			System.Drawing.Size size = GetChanageSize(width, height, FRAME_WIDTH, FRAME_HEIGHT);
			attr.Width = size.Width;
			
			attr.Height = size.Height;
			attr.Value1 = KunlunProduct.GetProductModel(WebLog.DivFrameID).Kacaprice;
			ViewBag.Attribute = attr;
			ViewBag.PhotoWidth = width;
			ViewBag.PhotoHeight = height;
			return View();
		}

		public ActionResult Photo(string productpicUrl,int w,int h) {
			ViewData["productpicUrl"] = productpicUrl ?? "/Content/images/PFScroll1.jpg";
			ViewData["Width"] = w;
			ViewData["Height"] = h;
			return PartialView("Photo");
		}

		
		public ActionResult SelectPhoto() {
			int userid = Convert.ToInt32(Request.Cookies.Get("userId").Value);
			ViewData["Dirs"] = Kunlun.BusinessLogic.KunlunLibrary.GetLibrary(userid);
			return PartialView("SelectPhoto");
		}

		[Authorize]
		public ActionResult DirPhoto(int dirID,int index,int w,int h) {
			int count;
			List<Consumer_pic> pic = Kunlun.BusinessLogic.KunlunLibrary.GetPic(dirID, index, 6, w * 100, h * 100, out count);
			System.Text.StringBuilder html = new System.Text.StringBuilder();
			foreach (Consumer_pic p in pic) {
				html.Append("<div>" + Kunlun.BusinessLogic.KunlunHtml.Image("", p.ThumUrl, WebLog.PhotoView, p.ImageAlt, new { onload = "DrawImage(this,86,86)", onclick = "ChangePhoto('" + WebLog.PhotoView + p.ListPicUrl + "'," + p.Width + "," + p.Height + "," + p.ID + ")" }) + "</div>");
			}

			int countPage = count / 6 + (count % 6 == 0 ? 0 : 1);
			html.Append("<div class='page'>");
			if (index == 0) {
				html.Append("<a>上页</a> ");
			} else {
				html.Append("<a href='javascript:var i=0;' onclick='LoadPhoto(" + (index - 1) + ")'>上页</a> ");
			}
			if (index < countPage - 1) {
				html.Append("<a href='javascript:var i=0;' onclick='LoadPhoto(" + (index + 1) + ")'>下页</a>");
			} else {
				html.Append("<a>下页</a>");
			}
			html.Append("</div>");
			return Content(html.ToString());
		}

		[Authorize]
		public ActionResult SaveFrameDir(int wID,string info,int PhotoID,int w,int h,int clipx,int clipy,int clipw,int cliph) {
			int userid = Convert.ToInt32(Request.Cookies.Get("userId").Value);

			KunLunDirModels.Model.DivFrame frame = new KunLunDirModels.Model.DivFrame();
			frame.ClipX = clipx;
			frame.ClipY = clipy;
			frame.ClipW = clipw;
			frame.ClipH = cliph;
			frame.FrameW = w;
			frame.FrameH = h;
			frame.ProductID = PhotoID;
			frame.ProjectName = w + "x" + h + "自定义框画";
			frame.FrameItems = new List<KunLunDirModels.Model.DivFrameItem>();
			foreach (string item in info.Split('|')) {
				KunLunDirModels.Model.DivFrameItem cart = new KunLunDirModels.Model.DivFrameItem();
				string[] str = item.Split(',');
				cart.PID = str[0];
				cart.Level = Convert.ToInt32(str[3]);
				cart.Size = Convert.ToInt32(str[4]);
				frame.FrameItems.Add(cart);
			}
			if (wID == 0) {
				wID = new FlexSave().SaveFrameWork(userid, PhotoID, w, h, frame);
			} else {
				new FlexSave().UpdateFrameWork(wID, PhotoID, w, h, frame);
				
			}
			return Content(wID.ToString());
		}

		public ActionResult AddCart(int workID) {
			//Kunlun.BusinessLogic.KunlunCarts.InsertCart(
			Works w = new FlexSave().GetWork(workID);
			int index = Kunlun.BusinessLogic.KunlunCarts.MaxItems();
			Kunlun.BusinessLogic.KunlunCarts.InsertCart(w.ProductID, 0, FrameType.框画, 0, 0, index, workID, 1);
			string xml = System.IO.File.ReadAllText(WebLog.Works+w.XMLPath);
			DivFrame frame = KunLunDirModels.SerializerBuilder.SerilizeAnObject(xml,typeof(DivFrame)) as DivFrame;
			foreach (DivFrameItem item in frame.FrameItems) {
				Kunlun.BusinessLogic.KunlunCarts.InsertCart(item.PID, 0, 0, (PaperLevel)item.Level, (PaperSize)item.Size, index, workID, 1);
			}
			return Json(true, JsonRequestBehavior.AllowGet);
		}

		[Authorize]
		public ActionResult DeleteWork(int workID) {
			int userid = Convert.ToInt32(Request.Cookies.Get("userId").Value);
			Works w = new Kunlun.BusinessLogic.FlexSave().DeleteWork(workID, userid);

			return Json(w != null, JsonRequestBehavior.AllowGet);
		}

		private System.Drawing.Size GetChanageSize(int sw, int sh, int mw, int mh) {
			double wc = (double)mw / sw;
			double hc = (double)mh / sh;
			if (wc < hc) {
				sw = mw;
				sh = (int)(sh * wc);
			} else {
				sh = mh;
				sw = (int)(sw * hc);
			}
			return new System.Drawing.Size(sw, sh);
		}




    }
}
